﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 文件名：ClsDeptSetDAL.cs
/// 作  者：SAM
/// 时  间：2006.11.10
/// 说  明：ClsDeptSetDAL类用于部门信息相关处理
/// </summary>
public class ClsDeptSetDAL
{
    /// <summary>
    /// 数据库连接字符串
    /// </summary>
    private static string strSqlConnectionString = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];

    /// <summary>
    /// 获取所有信息
    /// </summary>
    /// <returns>返回数据集</returns>
    public DataSet GetDeptSets()
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);

        SqlDataAdapter dat = new SqlDataAdapter("Pro_GetDeptSets", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 用于查询窗口获取数据
    /// </summary>
    /// <returns></returns>
    public DataSet SearchDeptSetByQueryFrom(string UserID, int Type)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_QueryWinGetDataByDept", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@u_log_id", SqlDbType.NVarChar, 60);
        dat.SelectCommand.Parameters["@u_log_id"].Value = UserID;
        dat.SelectCommand.Parameters.Add("@type", SqlDbType.Int, 4);
        dat.SelectCommand.Parameters["@type"].Value = Type;

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 模糊搜索部门信息
    /// </summary>
    /// <param name="Key">搜索关键字</param>
    /// <returns>返回数据集</returns>
    public DataSet SearchDeptSets(string Key)
    {
        DataSet dst = new DataSet();
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlDataAdapter dat = new SqlDataAdapter("Pro_SearchDeptSets", con);

        dat.SelectCommand.CommandType = CommandType.StoredProcedure;
        dat.SelectCommand.Parameters.Add("@key", Key);

        try
        {
            dat.Fill(dst);
        }
        catch
        {
            dst = null;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        return dst;
    }

    /// <summary>
    /// 作废部门信息
    /// </summary>
    /// <param>部门编号</param>
    /// <param name="Is_Visible">作废状态1为临时作废，－1为彻底作废</param>
    /// <returns>返回作废是否成功</returns>
    public Boolean DeleteDeptSet(string ID, int Is_Visible)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_DeleteDeptSet", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@dp_id", SqlDbType.NVarChar, 20);
        com.Parameters["@dp_id"].Value = ID;
        com.Parameters.Add("@is_visible", SqlDbType.Int, 1);
        com.Parameters["@is_visible"].Value = Is_Visible;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 确认部门信息
    /// </summary>
    /// <param>部门编号</param>
    /// <param name="AuditStat">确认状态</param>
    /// <param name="Auditor">确认人</param>
    /// <returns>返回确认是否成功</returns>
    public Boolean AuditStatDeptSet(string ID, int AuditStat, string Auditor)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AuditStatSetDeptSet", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@dp_id", SqlDbType.NVarChar, 20);
        com.Parameters["@dp_id"].Value = ID;
        com.Parameters.Add("@is_audit", SqlDbType.Int);
        com.Parameters["@is_audit"].Value = AuditStat;
        com.Parameters.Add("@auditor", SqlDbType.NVarChar, 60);
        com.Parameters["@auditor"].Value = Auditor;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 增加部门信息
    /// </summary>
    /// <param>部门编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    public Boolean AddDeptSet(string ID, string Name, string Manager, string Tel, string WhID,
        int iDp_statu,int iIs_Office,string Remark, string Oper)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_AddDeptSet", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@dp_id", SqlDbType.NVarChar, 20);
        com.Parameters["@dp_id"].Value = ID;
        com.Parameters.Add("@dp_name", SqlDbType.NVarChar, 20);
        com.Parameters["@dp_name"].Value = Name;
        com.Parameters.Add("@dp_manager", SqlDbType.NVarChar, 20);
        com.Parameters["@dp_manager"].Value = Manager;
        com.Parameters.Add("@dp_tel", SqlDbType.NVarChar, 20);
        com.Parameters["@dp_tel"].Value = Tel;
        com.Parameters.Add("@wh_id", SqlDbType.NVarChar, 20);
        com.Parameters["@wh_id"].Value = WhID;
        //中间插入各表的数据和参数

        com.Parameters.Add("@oper", SqlDbType.NVarChar, 60);
        com.Parameters["@oper"].Value = Oper;
        com.Parameters.Add("@Dp_statu", SqlDbType.Int);
        com.Parameters["@Dp_statu"].Value = iDp_statu;
        com.Parameters.Add("@Is_Office", SqlDbType.Int);
        com.Parameters["@Is_Office"].Value = iIs_Office;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 修改部门信息
    /// </summary>
    /// <param>部门编号</param>
    /// <param>......</param>
    /// <returns>返回修改是否成功</returns>
    public Boolean UpDateDeptSet(string ID, string Name, string Manager, string Tel, string WhID,
        int iDp_statu,int iIsOffice, string Remark)
    {
        SqlConnection con = new SqlConnection(strSqlConnectionString);
        SqlCommand com = new SqlCommand("Pro_UpDateDeptSet", con);

        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@dp_id", SqlDbType.NVarChar, 20);
        com.Parameters["@dp_id"].Value = ID;
        com.Parameters.Add("@dp_name", SqlDbType.NVarChar, 20);
        com.Parameters["@dp_name"].Value = Name;
        com.Parameters.Add("@dp_manager", SqlDbType.NVarChar, 20);
        com.Parameters["@dp_manager"].Value = Manager;
        com.Parameters.Add("@dp_tel", SqlDbType.NVarChar, 20);
        com.Parameters["@dp_tel"].Value = Tel;
        com.Parameters.Add("@wh_id", SqlDbType.NVarChar, 20);
        com.Parameters["@wh_id"].Value = WhID;
        
        //中间插入各表的数据和参数
        com.Parameters.Add("@Dp_statu", SqlDbType.Int);
        com.Parameters["@Dp_statu"].Value = iDp_statu;
        com.Parameters.Add("@IsOffice", SqlDbType.Int);
        com.Parameters["@IsOffice"].Value = iIsOffice;
        com.Parameters.Add("@remark", SqlDbType.NVarChar, 255);
        com.Parameters["@remark"].Value = Remark;

        try
        {
            con.Open();
            com.ExecuteNonQuery();

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

}
